package cn.itcast.health.provider.service.impl;

import cn.itcast.health.commons.entity.QueryPageBean;
import cn.itcast.health.commons.entity.Result;
import cn.itcast.health.commons.resources.MessageConstant;
import cn.itcast.health.interfaces.service.CheckitemService;
import cn.itcast.health.mapper.CheckitemMapper;
import cn.itcast.health.pojo.CheckItem;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CheckitemServiceImpl implements CheckitemService {

    @Autowired
    private CheckitemMapper checkitemMapper;

    @Override
    public Result addCheckItemData(CheckItem checkItem) {
        Result result = null;
        try {
            checkitemMapper.addCheckItemData(checkItem);
            result = new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        }catch (Exception e){
            result = new Result(true, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return result;
    }

    @Override
    public Result findByPageCheckitemData(QueryPageBean queryPageBean) {
        Result result = null;
        try {
            //设置分页条件
            PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
            //查询数据
            List<CheckItem> checkItems = checkitemMapper.findByPageCheckitemData(queryPageBean.getQueryString());
            PageInfo pageInfo = new PageInfo(checkItems);
            Map<String,Object> map = new HashMap();
            map.put("list",pageInfo.getList());
            map.put("total",pageInfo.getTotal());
            result = new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,map);
        }catch (Exception e){
            result = new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return result;
    }

    @Override
    public Result updateCheckItemData(CheckItem checkItem) {
        Result result = null;
        try {
            checkitemMapper.updateCheckItemData(checkItem);
            result = new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }catch (Exception e){
            result = new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return result;
    }

    @Override
    public Result deleteByIdCheckItemData(Integer id) {
        Result result = null;
        try {
            checkitemMapper.deleteByIdCheckItemData(id);
            result = new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }catch (Exception e){
            result = new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return result;
    }

    @Override
    public Result findCheckitemAll() {
        Result result = null;
        try{
            List<CheckItem> checkItems = checkitemMapper.findCheckitemAll();
            result = new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItems);
        }catch (Exception e){
            result = new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return result;
    }
}
